.PHONY: init
# 初始化开发环境
init:
	go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
	# https://github.com/favadi/protoc-go-inject-tag
	go install github.com/favadi/protoc-go-inject-tag@latest
	go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
	go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
	go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
	go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest
	go install github.com/google/wire/cmd/wire@latest

.PHONY: conf
# 编译 *.proto 配置文件(每次修改 conf.proto 后需要执行)
conf:
	protoc --experimental_allow_proto3_optional \
		   -I . \
		   --proto_path=. \
	       --proto_path=./third_party \
 	       --go_out=paths=source_relative:. \
	       ./${CONF_PROTO_PATH}/*.proto;
	# 注入 tag
	make tag;

.PHONY: tag
# 为生成的 *.pb.go 文件注入自定义 tag
tag:
	protoc-go-inject-tag -input=./${CONF_PROTO_PATH}/*.pb.go

.PYHONY: server-conf
server-conf: export CONF_PROTO_PATH=kratos-server/internal/conf
server-conf: conf

.PYHONY: client-conf
client-conf: export CONF_PROTO_PATH=kratos-client/internal/conf
client-conf: conf

.PHONY: server-api
# 编译 *.proto 的 api 文件
server-api:
	protoc --experimental_allow_proto3_optional \
		   -I . \
	       --proto_path=./third_party \
 	       --go_out=paths=source_relative:. \
 	       --go-grpc_out=paths=source_relative:. \
	       --openapi_out=fq_schema_naming=true,default_response=false:. \
	       ./kratos-server/api/v1/*.proto

.PHONY: client-api
# 编译 *.proto 的 api 文件
client-api:
	protoc --experimental_allow_proto3_optional \
		   -I . \
	       --proto_path=./third_party \
 	       --go_out=paths=source_relative:. \
 	       --go-http_out=paths=source_relative:. \
	       --openapi_out=fq_schema_naming=true,default_response=false:. \
	       ./kratos-client/api/v1/*.proto

.PHONY: wire
# wire
wire:
	wire ./${WIRE_PATH}

.PYHONY: server-wire
server-wire: export WIRE_PATH=kratos-server/cmd
server-wire: wire

.PYHONY: client-wire
client-wire: export WIRE_PATH=kratos-client/cmd
client-wire: wire

.PHONY: server-all
server-all: server-conf server-api server-wire

.PHONY: client-all
server-all: client-conf client-api client-wire

.PHONY: all
all:
	make server-all;
	make client-all;
	go vet ./...;

.PHONY: ent
ent:
	go generate ./kratos-server/internal/data/ent

# show help
help:
	@echo ''
	@echo 'Usage:'
	@echo ' make [target]'
	@echo ''
	@echo 'Targets:'
	@awk '/^[a-zA-Z\-\_0-9]+:/ { \
	helpMessage = match(lastLine, /^# (.*)/); \
		if (helpMessage) { \
			helpCommand = substr($$1, 0, index($$1, ":")); \
			helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
			printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
		} \
	} \
	{ lastLine = $$0 }' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help
